/*
 * Copyright (c) 2016. For Intelligent Group.
 */

package com.intelligent.ispc.client;

import com.intelligent.ispc.client.core.view.LoginPanel;
import com.intelligent.ispc.client.core.view.MainPanel;
import com.intelligent.ispc.client.core.view.global.MainTopMenuBar;
import com.intelligent.ispc.client.event.*;
import com.intelligent.ispc.client.utils.EventBusUtils;
import com.intelligent.ispc.client.utils.WindowUtils;
import com.intelligent.ispc.utils.ServerEventBusUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;

/**
 * Created by Peter on 2016/7/6.
 */
@org.springframework.stereotype.Component
public class MainFrame extends JFrame {

    private static final Dimension LOGIN_PANEL_SIZE = new Dimension(470, 220);
    private static final int FRAME_PANEL_SIZE_WIDTH = 1024;
    private static final int FRAME_PANEL_SIZE_HEIGHT = 768;

    @Autowired
    private LoginPanel loginPanel;

    @Autowired
    private MainTopMenuBar mainTopMenuBar;

    @Autowired
    private MainPanel mainPanel;

    /**
     * Constructor
     */
    public MainFrame() {
        initEvents();
    }

    /**
     * Initial components.
     */
    @PostConstruct
    public void init() {
        MainFrame frame = this;

        initComponents();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                frame.getContentPane().add(loginPanel);
                frame.pack();
                WindowUtils.centerWindowOnScreen(frame);
                frame.setVisible(true);
                Thread.currentThread().interrupt();
            }
        });
    }

    /**
     * Change panel with the class supported on Main Content Panel.
     *
     * @param panel panel Class
     */
    public void changePanel(Class panel) {
        if (MainPanel.class.equals(panel)) {
            this.getContentPane().removeAll();
            this.initMenuBar();
            this.setSize(FRAME_PANEL_SIZE_WIDTH, FRAME_PANEL_SIZE_HEIGHT);
            WindowUtils.centerWindowOnScreen(this);
            mainPanel.getMainContentPanel().defaultPanel();
            this.getContentPane().add(mainPanel);
            this.getContentPane().validate();
            this.getContentPane().repaint();
        } else if (LoginPanel.class.equals(panel)) {
            this.getContentPane().removeAll();
            this.remove(mainTopMenuBar);
            this.setJMenuBar(null);
            this.setPreferredSize(LOGIN_PANEL_SIZE);
            this.pack();
            this.repaint();
            WindowUtils.centerWindowOnScreen(this);
            this.getContentPane().add(loginPanel);
            this.getContentPane().validate();
            this.getContentPane().repaint();
        }
    }

    private void initComponents() {
        this.setTitle("iSPC Desktop");
        this.setIconImage(new ImageIcon(getClass().getResource("/icons/icon_app.png")).getImage());
        this.getContentPane().add(loginPanel);

    }

    private void initMenuBar() {
        this.setJMenuBar(mainTopMenuBar);
    }

    private void initEvents() {
        EventBusUtils.register(new StatusBarListener());
        EventBusUtils.register(new PanelChangeListener());
        EventBusUtils.register(new AnalysisFileChangeListener());
        EventBusUtils.register(new ExecutorTaskFinishListener());
        EventBusUtils.register(new ExportBtnListener());
        EventBusUtils.register(new SearchParamsChangeListener());
        EventBusUtils.register(new SearchItemDoubleClickListener());
        EventBusUtils.register(new SearchItemOneClickListener());
        EventBusUtils.register(new ExceptionCatchListener());
        ServerEventBusUtils.register(new TaskProcessChangeListener());
    }
}
